package com.etmvc.service.auth;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.etmvc.tools.AuthUtils;

public class SpaceService {
	public List<Map<String, Object>> getMenuUrlsTreeData(String authBasePath){
		Map<String,List<String>> allUrls = AuthUtils.getUrls(authBasePath,true);
		List<Map<String, Object>> records = new ArrayList<Map<String,Object>>();
		for (Entry<String, List<String>> e : allUrls.entrySet()) {
			String controller[] = e.getKey().split("#");
			Map<String, Object> row = new HashMap<String, Object>();
			row.put("text", controller[1]);
			row.put("state", "closed");
			List<Map<String, Object>> children = new ArrayList<Map<String,Object>>();
			for (String v : e.getValue()) {
				Map<String, Object> child = new HashMap<String, Object>();
				String actions[] = v.split("#");
				child.put("text", actions[1]);
				child.put("id", controller[0]+"/"+actions[0]);
//				child.put("", value);
				children.add(child);
			}
			row.put("children", children.toArray());//bug：etmvc自带的json转换并不好用，这里如果不toArray不能发生转换！
			records.add(row);
		}
		return records;
	}
	public List<Map<String, Object>> getAllUrlsTreeData(String authBasePath){
		Map<String,List<String>> allUrls = AuthUtils.getUrls(authBasePath,false);
		List<Map<String, Object>> records = new ArrayList<Map<String,Object>>();
		for (Entry<String, List<String>> e : allUrls.entrySet()) {
			String controller[] = e.getKey().split("#");
			Map<String, Object> row = new HashMap<String, Object>();
			row.put("text", controller[1]+"["+controller[0]+"]");
			row.put("state", "closed");
			List<Map<String, Object>> children = new ArrayList<Map<String,Object>>();
			for (String v : e.getValue()) {
				Map<String, Object> child = new HashMap<String, Object>();
				String actions[] = v.split("#");
				String text = actions[1]+"["+actions[0]+"]";
				if (actions.length==3) {//isMenu
					text = "<font color=red>"+actions[1]+"["+actions[0]+"]"+"<font>";
				}
				child.put("text", text);
				child.put("id", controller[0]+"/"+actions[0]);
//				child.put("", value);
				children.add(child);
			}
			row.put("children", children.toArray());//bug：etmvc自带的json转换并不好用，这里如果不toArray不能发生转换！
			records.add(row);
		}
		return records;
	}
}
